using System.Collections.Generic;
using System.Linq;
using System.Windows;
using DeSleeper.Client.Installation;

namespace DeSleeper.Client.Service
{
    internal class InstallServiceParameters : Freezable
	{
	    #region IsInstalled Dependency Property
	    /// <summary>
	    /// Is a version of 
	    /// </summary>
	    public bool IsInstalled
	    {
	        get { return (bool) GetValue(IsInstalledProperty); }
	        set { SetValue(IsInstalledProperty, value); }
	    }

	    /// <summary>
	    /// <see cref="IsInstalled"/> Dependency Property.
	    /// </summary>
	    public static readonly DependencyProperty IsInstalledProperty =
	        DependencyProperty.Register(
	            "IsInstalled",
	            typeof (bool),
	            typeof (InstallServiceParameters));
	    #endregion

        #region SuppressCheckForDotNet Dependency Property
        /// <summary>
        /// Assume that dotNet is installed remotely and proceed with installation even if
        /// remote registry cannot be accessed.
        /// </summary>
        public bool SuppressCheckForDotNet
        {
            get { return (bool) GetValue(SuppressCheckForDotNetProperty); }
            set { SetValue(SuppressCheckForDotNetProperty, value); }
        }

        /// <summary>
        /// <see cref="SuppressCheckForDotNet"/> Dependency Property.
        /// </summary>
        public static readonly DependencyProperty SuppressCheckForDotNetProperty =
            DependencyProperty.Register(
                "SuppressCheckForDotNet",
                typeof (bool),
                typeof (InstallServiceParameters));
        #endregion

	    #region Options Dependency Property
	    /// <summary>
	    /// Options that modify how installation proceeds.  The same values will be used
	    /// for all targets.
	    /// </summary>
	    public InstallServiceOptions Options
	    {
	        get { return (InstallServiceOptions) GetValue(OptionsProperty); }
	        set { SetValue(OptionsProperty, value); }
	    }

	    /// <summary>
	    /// Options Dependency Property.
	    /// </summary>
	    public static readonly DependencyProperty OptionsProperty =
	        DependencyProperty.Register(
	            "Options",
	            typeof (InstallServiceOptions),
	            typeof (InstallServiceParameters));
	    #endregion

	    #region PrecacheHostNamesString Dependency Property
	    /// <summary>
	    /// What hosts should the installer attempt to cache in the local MAC to HostName
	    /// table?
	    /// </summary>
	    public string PrecacheHostNamesString
	    {
	        get { return (string) GetValue(PrecacheHostNamesStringProperty); }
	        set { SetValue(PrecacheHostNamesStringProperty, value); }
	    }

	    /// <summary>
	    /// <see cref="PrecacheHostNamesString"/> Dependency Property.
	    /// </summary>
	    public static readonly DependencyProperty PrecacheHostNamesStringProperty =
	        DependencyProperty.Register(
	            "PrecacheHostNamesString",
	            typeof (string),
	            typeof (InstallServiceParameters),
	            new PropertyMetadata(new PropertyChangedCallback(PrecacheHostNamesStringChanged)));

	    private static void PrecacheHostNamesStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	    {
	        var parameters = d as InstallServiceParameters;
	        if (parameters != null)
	            parameters._precacheHostNames = null;
	    }

        private IEnumerable<string> _precacheHostNames;
	    public IEnumerable<string> PrecacheHostNames
	    {
	        get
	        {
                if (_precacheHostNames == null)
                {
                    _precacheHostNames = string.IsNullOrEmpty(PrecacheHostNamesString)
                                             ? new List<string>()
                                             : PrecacheHostNamesString.Split(',').ToList();
                }
	            return _precacheHostNames;
	        }
        }
        #endregion
	    #region InstallTargetsString Dependency Property
	    /// <summary>
	    /// What hosts should the installer attempt to install the service to.  If none
	    /// specified a local installation will be performed.  To perform local and remote
	    /// use "localhost" to refer to the local host.
	    /// </summary>
	    public string InstallTargetsString
	    {
	        get { return (string) GetValue(InstallTargetsStringProperty); }
	        set { SetValue(InstallTargetsStringProperty, value); }
	    }

	    /// <summary>
	    /// <see cref="InstallTargetsString"/> Dependency Property.
	    /// </summary>
	    public static readonly DependencyProperty InstallTargetsStringProperty =
	        DependencyProperty.Register(
                "InstallTargetsString",
	            typeof (string),
	            typeof (InstallServiceParameters),
	            new PropertyMetadata(new PropertyChangedCallback(InstallTargetsStringChanged)));

	    private static void InstallTargetsStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	    {
	        var parameters = d as InstallServiceParameters;
	        if (parameters != null)
	            parameters._installTargets = null;
	    }

        private InstallTargets _installTargets;
        public InstallTargets InstallTargets
	    {
	        get
	        {
                if (_installTargets == null)
                {
                    _installTargets = new InstallTargets(InstallTargetsString);
                }
	            return _installTargets;
	        }
        }
        #endregion

        protected override Freezable CreateInstanceCore()
	    {
	        return new InstallServiceParameters();
	    }
	}
}